/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.TexturePaint;
import java.awt.Toolkit;
import java.awt.Transparency;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.util.Observable;
import javax.swing.JFrame;
import javax.swing.JPanel;
import modelo.Hora;
import modelo.Minutos;


/**
 *
 * @author Depto
 */
public class FrameReloj extends JFrame {

    /**
     * Creates new form FrameReloj
     */
    
    private Image relojView;
	
    // es para almacenar la imagen de la bola y no tener que leer la de disco cada vez
    private BufferedImage relojImage;

    private Hora miHora;
    
    private Minutos misMinutos;
    
    
    private BufferedImage background,backgroundTile;
    private SpriteCache spriteCache;
    private JPanel panel;
    
        private javax.swing.JButton btnAceptar;
    private javax.swing.JComboBox cmbHora;
    private javax.swing.JComboBox cmbMinutos;
    private javax.swing.JLabel lblHora;
    private javax.swing.JLabel lblMinutos;
    private ImagePanel panelReloj;
    
    public FrameReloj(Hora h,Minutos m) {
		super("Tell me the time...");
		miHora = h;
		misMinutos = m;
                spriteCache = new SpriteCache();
                initComponents();
                pintarComponentes();
                
                
                
    }
    
    


    public void pintarComponentes(){
                    cmbHora = new javax.swing.JComboBox();
        lblHora = new javax.swing.JLabel();
        lblMinutos = new javax.swing.JLabel();
        cmbMinutos = new javax.swing.JComboBox();
        btnAceptar = new javax.swing.JButton();
        btnAceptar.addActionListener(new ActionListener() {
 
            public void actionPerformed(ActionEvent e)
            {
                        Hora h = new Hora(Integer.parseInt(cmbHora.getSelectedItem().toString()));
                        miHora = h;
                        System.out.println("action preformed");
                	 Minutos m = new Minutos(Integer.parseInt(cmbMinutos.getSelectedItem().toString()));
                        misMinutos = m;
                	panelReloj.setHoraMinutos(miHora,misMinutos);
            }
        });    
        panelReloj = new ImagePanel(new FlowLayout(FlowLayout.CENTER,50,180),miHora,misMinutos);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        cmbHora.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" }));

        lblHora.setText("Hora:");

        lblMinutos.setText("Minutos");

        cmbMinutos.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59" }));

        btnAceptar.setText("Aceptar");

        javax.swing.GroupLayout panelRelojLayout = new javax.swing.GroupLayout(panelReloj);
        panelReloj.setLayout(panelRelojLayout);
        panelRelojLayout.setHorizontalGroup(
            panelRelojLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        panelRelojLayout.setVerticalGroup(
            panelRelojLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(panelReloj, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 121, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnAceptar)
                        .addGap(23, 23, 23))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(cmbHora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(lblHora)
                                .addComponent(lblMinutos))
                            .addComponent(cmbMinutos, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(139, 139, 139)
                .addComponent(lblHora)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cmbHora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblMinutos)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(cmbMinutos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnAceptar)
                .addContainerGap(224, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(panelReloj, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(72, 72, 72))
        );

        pack();
    }
    

    public void center() {
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = getSize();
		int x = (screenSize.width - frameSize.width) / 2;
		int y = (screenSize.height - frameSize.height) / 2;
		setLocation(x, y);
	}

	public void paint(Graphics g){
            
            pintarComponentes();
        }
	
	public void paintTiempo(Graphics g){
		Dimension d = this.panelReloj.getSize();
		int[] xPoints = new int[3];
		int[] yPoints = new int[3];
		
		
		int dx = 8;
		double ang = ((90*2*Math.PI)/360);
		
		int x0 = (int) Math.round(miHora.getX1() - (Math.sin(ang - miHora.getAngulo())*dx));
		int x1 = (int) Math.round(miHora.getX1() + (Math.sin(ang - miHora.getAngulo())*dx));
		int y0 = (int) Math.round(miHora.getY1() + (Math.cos(ang - miHora.getAngulo())*dx));
		int y1 = (int) Math.round(miHora.getY1() - (Math.cos(ang - miHora.getAngulo())*dx));
		
		xPoints[0] = x0;
		xPoints[1] = x1;
		xPoints[2] = miHora.getX2();
		yPoints[0] = d.height-y0;
		yPoints[1] = d.height-y1;
		yPoints[2] = d.height-miHora.getY2();
		//g.drawImage(horaImage, x0, y0, x1, y1, x0 + (int) Math.round(miHora.getLength()*Math.sin(miHora.getAngulo())), (int) Math.round(y0 + miHora.getLength()*Math.cos(miHora.getAngulo())), (int) Math.round(x1 + miHora.getLength()*Math.sin(miHora.getAngulo())),  (int) Math.round(y1 + miHora.getLength()*Math.cos(miHora.getAngulo())), null);
		g.fillPolygon(xPoints, yPoints, 3);
		
		
		int[] xMinPoints = new int[3];
		int[] yMinPoints = new int[3];
		
		int xMin0 = (int) Math.round(misMinutos.getX1() - (Math.sin(ang - misMinutos.getAngulo())*dx));
		int xMin1 = (int) Math.round(misMinutos.getX1() + (Math.sin(ang - misMinutos.getAngulo())*dx));
		int yMin0 = (int) Math.round(misMinutos.getY1() + (Math.cos(ang - misMinutos.getAngulo())*dx));
		int yMin1 = (int) Math.round(misMinutos.getY1() - (Math.cos(ang - misMinutos.getAngulo())*dx));
		
		xMinPoints[0] = xMin0;
		xMinPoints[1] = xMin1;
		xMinPoints[2] = misMinutos.getX2();
		yMinPoints[0] = d.height-yMin0;
		yMinPoints[1] = d.height-yMin1;
		yMinPoints[2] = d.height-misMinutos.getY2();
		g.fillPolygon(xMinPoints, yMinPoints, 3);
	}

	// es llamado internamente por el metodo repaint() de la clase Frame
    @Override
	public void update(Graphics g) {
		paint(g);
	}

    public void update(Observable o, Object arg) {
        repaint();
        
    }
    

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 641, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 500, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
